<?php
namespace cate\alias;

Class Alias 
{
    protected static $instance;

    protected $registered = false;

    protected $aliases = [ 'Iphone' => 'cate\alias\Iphone',];

    static function getInstance()
    {
        if (is_null(static::$instance)) {
            return static::$instance = new static;
        }
    
        return static::$instance;
    }

    function aliases($alias)
    {
        echo "spl_autoload_register aliases className=$alias \n";
        if (isset($this->aliases[$alias])) {
            echo "exist\n";
            return class_alias($this->aliases[$alias], $alias);
        }
        echo "not exits\n";
    }

        /**
     * Register the loader on the auto-loader stack.
     *
     * @return void
     */
    public function register()
    {
        if (! $this->registered) {
            $this->loader();

            $this->registered = true;
        }
    }

    public function loader()
    {
        spl_autoload_register([$this, 'aliases'],false,true);
        spl_autoload_register([$this, 'baseLoad'],false,true);
    }

    public function baseLoad($name)
    {
        echo 'spl_autoload_register baseLoads name:', $name, "\n";
        $f =  preg_replace('~\\\\~', '/', $name) . '.php';
        $f = strrchr($f, '/');
        $f = substr($f, 1);
        
        var_dump('file:'. $f);
        if (is_file($f)) return include_once $f;

    }
}